package com.maixy.studyonlineapi.advice;


import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.bean.State;
import com.maixy.studyonlineapi.exceptions.ApiException;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.exceptions.NotExistException;
import com.maixy.studyonlineapi.exceptions.ParamException;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.exceptions.TokenException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName ExceptionControllerAdvice
 * @Description TODO
 * @Date 2020/4/13 10:38
 * @Author mai
 */
@RestControllerAdvice
@Slf4j
public class ExceptionControllerAdvice {

    @ExceptionHandler(TipException.class)
    public ApiResponse TipExceptionHandler(TipException e) {
        log.error("----------------------code:{}, msg:{}------------------------", e.getCode(), e.getMsg());
        return ApiResponse.error(new State(e.getCode(), e.getMsg()));
    }

    @ExceptionHandler(TokenException.class)
    public ApiResponse TokenExceptionHandler(TokenException e) {
        log.error("----------------------code:{}, msg:{}------------------------", e.getCode(), e.getMsg());
        return ApiResponse.error(new State(e.getCode(), e.getMsg()));
    }

    @ExceptionHandler(ApiException.class)
    public ApiResponse ApiExceptionHandler(ApiException e) {
        log.error("----------------------code:{}, msg:{}------------------------", e.getCode(), e.getMsg());
        return ApiResponse.error(new State(e.getCode(), e.getMsg()));
    }

    @ExceptionHandler(MissParamException.class)
    public ApiResponse MissParamExceptionHandler(MissParamException e) {
        log.error("----------------------code:{}, msg:{}------------------------", e.getCode(), e.getMsg());
        return ApiResponse.error(new State(e.getCode(), e.getMsg()));
    }

    @ExceptionHandler(ParamException.class)
    public ApiResponse ParamExceptionHandler(ParamException e) {
        log.error("----------------------code:{}, msg:{}------------------------", e.getCode(), e.getMsg());
        return ApiResponse.error(new State(e.getCode(), e.getMsg()));
    }

    @ExceptionHandler(NotExistException.class)
    public ApiResponse NotExistExceptionHandler(NotExistException e) {
        log.error("----------------------code:{}, msg:{}------------------------", e.getCode(), e.getMsg());
        return ApiResponse.error(new State(e.getCode(), e.getMsg()));
    }


}
